package people.paper.tv.activity.home.model;

import com.coocaa.app.support.net.Callback;
import com.coocaa.app.support.net.HttpServiceManager;
import com.coocaa.app.support.net.Result;
import com.coocaa.app.support.net.monitor.DefaultMonitorImpl;

import java.io.File;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import people.paper.tv.BaseApplication;
import people.paper.tv.utils.encrypt.SHA1;
import retrofit2.Call;

public class MainServerHttpService extends HttpServiceManager<MainServerMethod> implements MainServerMethod {

    private static class HttpDataEmptyException extends Exception {
        public HttpDataEmptyException() {
            super("HttpDataEmptyException");
        }
    }

    private static final HttpServiceManagerConfig config = new HttpServiceManagerConfig() {
        private HttpServiceMonitor monitor = new DefaultMonitorImpl();

        @Override
        public Map<String, String> loadHeaders() {
            Map<String, String> headers = new HashMap<>();
            headers.put("app_vn", BaseApplication.getVersionName());
            headers.put("app_vc", String.valueOf(BaseApplication.getVersionCode()));
            headers.put("app_pn", BaseApplication.getContext().getPackageName());
            return headers;
        }

        @Override
        public boolean isNetConnected() {
            return BaseApplication.isNetConnected();
        }

        @Override
        public HttpServiceMonitor getHttpServiceMonitor() {
            return monitor;
        }
    };
    public static final MainServerHttpService httpService = new MainServerHttpService();

    private MainServerHttpService() {
        super("cj.peopledigital.com.cn", new File(BaseApplication.getContext().getCacheDir(), "http_cache"), 100 * 1024 * 1024, config);
    }

    @Override
    protected Class<MainServerMethod> getServiceClazz() {
        return MainServerMethod.class;
    }

    @Override
    public Call<MainDataObject.RMRBData> postRMRB(String date, long timestamp, int nonce, String signature) {
        return getHttp().postRMRB(date, timestamp, nonce, signature);
    }

    @Override
    public Call<MainDataObject.RMRBContentData> postRMRBContent(String date, String layoutId, String newsId, long timestamp, int nonce, String signature) {
        return getHttp().postRMRBContent(date, layoutId, newsId, timestamp, nonce, signature);
    }

    @Override
    public Call<MainDataObject.SPTTData> postSPTT(int start, int end, long timestamp, int nonce, String signature) {
        return getHttp().postSPTT(start, end, timestamp, nonce, signature);
    }

    @Override
    public Call<MainDataObject.YWYLData> postYWYL(String type, int start, int end, long timestamp, int nonce, String signature) {
        return getHttp().postYWYL(type, start, end, timestamp, nonce, signature);
    }

    private SeqExecuteHandler<MainDataObject.YWYLData> getPicturesExecuteHandler = new SeqExecuteHandler();
    public static final int PAGE_SIZE = 36;

    public void getPictures(String category, int pageIndex, final Callback<Result<MainDataObject.YWYLData>> callback) {
        String type = category;
        int start = pageIndex * PAGE_SIZE;
        int end = (pageIndex + 1) * PAGE_SIZE;
        List<String> params = new ArrayList<>();
        params.add(String.valueOf(start));
        params.add(String.valueOf(end));
        params.add(type);
        Signature signature = getSignatur(params);
        Call<MainDataObject.YWYLData> call = MainServerHttpService.httpService.postYWYL(type, start, end, signature.timestamp, signature.nonce, signature.signature);
        getPicturesExecuteHandler.call(call, new Callback<Result<MainDataObject.YWYLData>>() {
            @Override
            public Ret onResult(Result<MainDataObject.YWYLData> result) {
                if (result != null &&
                        result.data != null &&
                        result.data.obj != null) {
                    return callback.onResult(Result.cc(result, result.data));
                } else {
                    Result result1 = new Result();
                    result1.exception = new HttpDataEmptyException();
                    return callback.onResult(result1);
                }
            }
        });
    }

    private SeqExecuteHandler<MainDataObject.SPTTData> getSPTTExecuteHandler = new SeqExecuteHandler();

    public void getSPTT(int pageIndex, final Callback<Result<List<MainDataObject.SPTTObject>>> callback) {
        int start = pageIndex * PAGE_SIZE;
        int end = (pageIndex + 1) * PAGE_SIZE;
        List<String> params = new ArrayList<>();
        params.add(String.valueOf(start));
        params.add(String.valueOf(end));
        Signature signature = getSignatur(params);

        Call<MainDataObject.SPTTData> call = MainServerHttpService.httpService.postSPTT(start, end, signature.timestamp, signature.nonce, signature.signature);
        getSPTTExecuteHandler.call(call, new Callback<Result<MainDataObject.SPTTData>>() {
            @Override
            public Ret onResult(Result<MainDataObject.SPTTData> result) {
                if (result != null &&
                        result.data != null &&
                        result.data.obj != null) {
                    return callback.onResult(Result.cc(result, result.data.obj));
                } else {
                    Result result1 = new Result();
                    result1.exception = new HttpDataEmptyException();
                    return callback.onResult(result1);
                }
            }
        });
    }

    public static class Signature {
        public long timestamp;
        public int nonce;
        public String signature;
    }

    public Signature getSignatur(List<String> params) {
        Signature signature = new Signature();
        signature.timestamp = System.currentTimeMillis();
        signature.nonce = (int) (Math.random() * Integer.MAX_VALUE);
        params.add(String.valueOf(signature.timestamp));
        params.add(String.valueOf(signature.nonce));
        params.add("2damHGwMhOcqk5JRisMl");
        String[] _params = new String[params.size()];
        _params = params.toArray(_params);
        Arrays.sort(_params);
        String sign = "";
        for (String key : _params)
            sign += key;
        signature.signature = SHA1.sha1(sign);
        return signature;
    }
}
